package com.hemaapp.yjnh.view;

import java.io.IOException;

import xtom.frame.XtomObject;
import xtom.frame.fileload.XtomFileDownLoader;
import xtom.frame.media.XtomVoicePlayer;
import xtom.frame.media.XtomVoicePlayer.XtomVoicePlayListener;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.TextView;

import com.buihha.audiorecorder.Mp3Recorder;
import com.buihha.audiorecorder.TimeThread;
import com.hemaapp.hm_FrameWork.HemaActivity;
import com.hemaapp.yjnh.R;

public class RecordView extends XtomObject {
	private static final int TOTALSECOND = 90;// 录音时长

	private HemaActivity mContext;
	private View rootView;
	private PopupWindow mWindow;
	private ViewGroup mViewGroup;
	private TextView second;
	private TextView totalsecond;
	private TextView mTextView;
	private ImageView mImageView;
	private ImageButton delete;
	private Button finishButton;

	private String filepath;
	private Mp3Recorder mRecorder;
	private RecordHandler recordHandler = new RecordHandler(this);

	private XtomVoicePlayer voicePlayer;

	private void recording(Message msg) {
		int s = msg.arg1 / 1000;
		if (s >= TOTALSECOND) {
			stopRecording();
			return;
		}
		mTextView.setText("正在录音");
		second.setText(s + "''");
		int i = (msg.arg1 / 100) % 8;
		switch (i / 2) {
		case 0:
			mImageView.setImageResource(R.drawable.img_recordview_recording_0);
			break;
		case 1:
			mImageView.setImageResource(R.drawable.img_recordview_recording_1);
			break;
		case 2:
			mImageView.setImageResource(R.drawable.img_recordview_recording_2);
			break;
		case 3:
			mImageView.setImageResource(R.drawable.img_recordview_recording_3);
			break;
		}
	}

	public RecordView(HemaActivity context) {
		mContext = context;
		mRecorder = new Mp3Recorder(mContext, recordHandler);
		rootView = context.findViewById(R.id.rootView);
		mWindow = new PopupWindow(mContext);
		mWindow.setWidth(LayoutParams.MATCH_PARENT);
		mWindow.setHeight(LayoutParams.MATCH_PARENT);
		mWindow.setBackgroundDrawable(new ColorDrawable(0x55000000));
		mWindow.setFocusable(true);
		mWindow.setAnimationStyle(R.style.PopupAnimation);
		mViewGroup = (ViewGroup) LayoutInflater.from(mContext).inflate(
				R.layout.recordview, null);
		findView();
		setListener();
		mWindow.setContentView(mViewGroup);

	}

	private void findView() {
		second = (TextView) mViewGroup.findViewById(R.id.second);
		totalsecond = (TextView) mViewGroup.findViewById(R.id.totalsecond);
		mImageView = (ImageView) mViewGroup.findViewById(R.id.imageview);
		delete = (ImageButton) mViewGroup.findViewById(R.id.delete);
		mTextView = (TextView) mViewGroup.findViewById(R.id.textview);
		finishButton = (Button) mViewGroup.findViewById(R.id.finish);
	}

	private void setListener() {
		mWindow.setOnDismissListener(new OnDismissListener() {

			@Override
			public void onDismiss() {
				stopRecording();
				stopPlay(true);
				if (recordListener != null) {
					int time = getTime();
					String duration = String.valueOf(time);
					recordListener.OnFinished(filepath, duration);
				}

			}
		});
		mViewGroup.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mWindow.dismiss();
			}
		});
		mImageView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (!isNull(filepath) && !mRecorder.isRecording()) {
					if (voicePlayer == null || !voicePlayer.isPlaying())
						startPlay();
					else
						stopPlay(false);
					return;
				}
				if (!mRecorder.isRecording())
					startRecording();
				else
					stopRecording();
			}
		});
		delete.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				resetAudio();
			}
		});
		finishButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mWindow.dismiss();
			}
		});
	}

	public void show() {
		mWindow.showAtLocation(rootView, Gravity.BOTTOM, 0, 0);
	}

	private void resetAudio() {
		filepath = null;
		second.setText(0 + "''");
		delete.setVisibility(View.INVISIBLE);
		mTextView.setText("点击录音");
		mImageView.setImageResource(R.drawable.img_recordview_record);
		delete.setVisibility(View.INVISIBLE);
		totalsecond.setText("(" + TOTALSECOND + "'')");
	}

	public void dimiss() {
		mWindow.dismiss();
	}

	private void stopRecording() {
		if (mRecorder != null && mRecorder.isRecording())
			try {
				mRecorder.stopRecording();
			} catch (IOException e) {
				e.printStackTrace();
			}
	}

	private void startRecording() {
		if (mRecorder != null && !mRecorder.isRecording())
			try {
				mRecorder.startRecording();
			} catch (IOException e) {
				e.printStackTrace();
			}
	}

	private void startPlay() {
		if (voicePlayer == null) {
			voicePlayer = new XtomVoicePlayer(mContext, filepath);
			voicePlayer.setXtomVoicePlayListener(new VoicePlayListener());
		}

		if (!voicePlayer.isPlaying()) {
			voicePlayer.start();
		}
	}

	private void stopPlay(boolean isRelease) {
		if (voicePlayer != null) {
			voicePlayer.stop();
			if (isRelease) {
				voicePlayer.release();
				voicePlayer = null;
			}
		}
	}

	public String getFilepath() {
		return filepath;
	}

	public int getTime() {
		int time = 0;
		if (mRecorder != null)
			time = mRecorder.getTime() / 1000;
		return time;
	}

	private class VoicePlayListener implements XtomVoicePlayListener {

		@Override
		public void loadStart(XtomVoicePlayer player, XtomFileDownLoader loader) {
			// TODO Auto-generated method stub

		}

		@Override
		public void loading(XtomVoicePlayer player, XtomFileDownLoader loader) {
			// TODO Auto-generated method stub

		}

		@Override
		public void loadSuccess(XtomVoicePlayer player,
				XtomFileDownLoader loader) {
			// TODO Auto-generated method stub

		}

		@Override
		public void loadFailed(XtomVoicePlayer player, XtomFileDownLoader loader) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onStart(XtomVoicePlayer player) {
			mTextView.setText("正在播放");
			mImageView.setImageResource(R.drawable.img_recordview_stop);
			delete.setVisibility(View.INVISIBLE);
		}

		@Override
		public void onPause(XtomVoicePlayer player) {

		}

		@Override
		public void onPlaying(XtomVoicePlayer player) {
			int current = player.getCurrentPosition();
			int s = current / 1000;
			second.setText(s + "''");
		}

		@Override
		public void onStop(XtomVoicePlayer player) {
			mTextView.setText("点击播放");
			mImageView.setImageResource(R.drawable.img_recordview_play);
			delete.setVisibility(View.VISIBLE);
		}

		@Override
		public void onComplete(XtomVoicePlayer player) {
			mTextView.setText("点击播放");
			mImageView.setImageResource(R.drawable.img_recordview_play);
			delete.setVisibility(View.VISIBLE);
		}

		@Override
		public void onError(XtomVoicePlayer player) {
			// TODO Auto-generated method stub

		}

	}

	private static class RecordHandler extends Handler {
		RecordView recordView;

		public RecordHandler(RecordView recordView) {
			this.recordView = recordView;
		}

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case TimeThread.ING:
				recordView.filepath = recordView.mRecorder.getFilePath();
				recordView.recording(msg);
				break;
			case TimeThread.STOP:
				recordView.mTextView.setText("点击播放");
				recordView.mImageView
						.setImageResource(R.drawable.img_recordview_play);
				recordView.delete.setVisibility(View.VISIBLE);
				recordView.filepath = recordView.mRecorder.getFilePath();
				recordView.totalsecond.setText("(" + msg.arg1 / 1000 + "'')");
				recordView.second.setText(msg.arg1 / 1000 + "''");
				break;
			default:
				break;
			}
		}
	}

	private RecordListener recordListener;

	/**
	 * @return the recordListener
	 */
	public RecordListener getRecordListener() {
		return recordListener;
	}

	/**
	 * @param recordListener
	 *            the recordListener to set
	 */
	public void setRecordListener(RecordListener recordListener) {
		this.recordListener = recordListener;
	}

	public interface RecordListener {
		void OnFinished(String voicePath, String duration);
	}
}
